
// https:/v2rich.cn/stock/get_realtime_price get {itype:'fund', codes: []}
// itype表示查询的类别   fund-基金   stock-股票  bond-债券  futures-期货
import { request } from '@/utils/request'

export type IType = 'fund' | 'stock' | 'bond' | 'futures' | 'ETFLOF'

export interface RealtimeParams {
  itype: IType,
  codes: string[]
}

export interface FiledValue {
  filed: string
  value: number | string
}

export interface FundData {
  A: FiledValue //  更新时间
  B: FiledValue // 最新价格-单位净值
  C: FiledValue // 最新价格-累计净值
  D: FiledValue // 涨跌幅
  code: string // 代码
}

export const realTimePriceReq = (data: RealtimeParams) => {
  return request<{ datalist: FundData[]; count: number }>({
    url: '/stock/get_realtime_price',
    method: 'post',
    data
  }).then(res => {
    return res.data.datalist[0]
  })
}

const REQUEST_CONFIG = {
  url: '/stock/get_realtime_price/',
  method: 'post'
}

export type ReqFun<DataType = unknown> = (codes: string[]) => Promise<DataType>

export const fundPriceReq = (codes: string[]) => {
  return request<{ datalist: FundData[]; count: number }>({
    ...REQUEST_CONFIG,
    data: {
      itype: 'fund',
      codes
    }
  }).then(res => {
    const { datalist } = res.data
    return datalist[0]
  })
}

export interface InnerData {
  C: FiledValue // 涨跌幅
  D: FiledValue // 最新价
  S: FiledValue // 最新时间
  code: string // 代码
}

export const etfPriceReq = (codes: string[]) => {
  return request<{ datalist: InnerData[]; count: number }>({
    ...REQUEST_CONFIG,
    data: {
      itype: 'ETFLOF',
      codes
    }
  }).then(res => {
    const { datalist } = res.data
    return datalist[0]
  })
}


export const bondPriceReq = (codes: string[]) => {
  return request<{ datalist: InnerData[]; count: number }>({
    ...REQUEST_CONFIG,
    data: {
      itype: 'bond',
      codes
    }
  }).then(res => {
    const { datalist } = res.data
    return datalist[0]
  })
}



export const stockPriceReq = (codes: string[]) => {
  return request<{ datalist: InnerData[]; count: number }>({
    ...REQUEST_CONFIG,
    data: {
      itype: 'stock',
      codes
    }
  }).then(res => {
    const { datalist } = res.data
    return datalist[0]
  })
}

